package com.ruoyi.pcj.docking.zego;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.mashuai.utils.toolkit.core.msHttp.MsHttpUtils;
import com.ruoyi.pcj.docking.zego.core.ZeGoRequest;
import com.ruoyi.pcj.docking.zego.dto.ZeGoUserInfoDto;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 所有请求参数（包括公共参数和业务参数）统⼀放在 Query，使⽤ GET 请求方法。特殊复杂的 API 参数放在 Body，使用 POST 请求方法。
 *
 * @ClassName: ZeGoUtils
 * @Author : mashuai
 * @Description: TODO
 */
public class ZeGoUtils {
    private static long appId = 792101497;    // 请替换为你的 appId，从即构控制台获取
    private static String serverSecret = "69c71969171b70b1c4d5e8e71a803a1d";  // 请替换为你的 serverSecret，从即构控制台获取，

    //---------请求---
    //URL
    private static final String ZE_GO_URL = "https://zim-api.zego.im/?";
    //获取用户在线状态
    private static final String url_QueryUserOnlineState = ZE_GO_URL + "Action=QueryUserOnlineState";
    //注册用户
    private static final String url_UserRegister = ZE_GO_URL + "Action=UserRegister";

    public static void main(String[] args) {
        getUserOnlineState();
    }

    public static void registerUserInfo() {
        Map<String, List<ZeGoUserInfoDto>> params = new HashMap<>();
        List<ZeGoUserInfoDto> list = Arrays.asList(
                new ZeGoUserInfoDto("937958191", "ms", "https://configservercdn-adm.cqttech.com/d/7/4/d7c49bbb99f1672b77ff289de8953df1.png")
        );
        params.put("UserInfo",list);
        String basicUrl = getBasicUrl(url_UserRegister);
        String result = MsHttpUtils.sendPostJSON(basicUrl, params);
        System.err.println(JSON.toJSONString(result));
    }


    /***
     * 获取用户在线状态
     */
    public static void getUserOnlineState() {
        //本次获取的用户id
        String basicUrl = getBasicUrl(url_QueryUserOnlineState) + "&UserId[]=937958191";
        //打印：https://zim-api.zego.im/?Action=QueryUserOnlineState&SignatureVersion=2.0&AppId=792101497&SignatureNonce=f95caffd5f395da9&Signature=ef819f3ea5e41bfdbf8112bd4e4eeadd&Timestamp=1710762148&UserId[]=937958191
        System.err.println(basicUrl);
        String result = MsHttpUtils.sendGet(basicUrl, null);
        System.err.println(JSON.toJSONString(result));
    }

    /***
     *获取用户token
     * @param userId 请替换为用户的 userID，同一 appId 下全网唯一
     * @return
     */
    public static String getToken(String userId) {
//        String token = ZeGoGetToken.getToken(appId, serverSecret, userId);
//        return token;
        return null;
    }


    /***
     * 获取每次请求拼接上带的公共参数url
     */

    private static String getBasicUrl(String url) {
        String newUrl = url + "&" + HttpUtil.toParams(ZeGoRequest.basicQueryParams(appId, serverSecret));
        return newUrl;
    }


}
